<?php
/**
 * DBShop 电子商务系统
 *
 * ==========================================================================
 * @link      https://www.dbshop.net/
 * @copyright 北京珑大钜商科技有限公司，并保留所有权利。
 * @license   https://www.dbshop.net/license.html License
 * ==========================================================================
 *
 * @author    静静的风 <baron@loongdom.cn>
 *
 */

namespace Operation\Form;

use Admin\Data\Common;
use Admin\Data\Config;
use Admin\Filter\ImageRegenerate;
use Admin\Filter\OldImage;
use Admin\Validator\ImageSuffixValidator;
use Laminas\Form\Form;

class AdForm extends Form
{
    private $adInfo;

    public function __construct($adInfo = null, $name = 'mobile-ad-edit-form', $options = [])
    {
        parent::__construct($name, $options);
        $this->setAttribute('method', 'post');
        $this->setAttribute('class', 'form-horizontal');

        $this->adInfo = $adInfo;

        $this->addElements();
        $this->addInputFilter();
    }

    protected function addElements()
    {
        $this->add([
            'type'  => 'text',
            'name'  => 'adUrl',
            'attributes'    => [
                'id'    => 'adUrl',
                'class' => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'text',
            'name'  => 'adWords',
            'attributes'    => [
                'id'    => 'adWords',
                'class' => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'file',
            'name'  => 'adImage',
            'attributes'    => [
                'id'    => 'adImage',
                'class' => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'text',
            'name'  => 'adStartTime',
            'attributes'    => [
                'id'    => 'adStartTime',
                'class' => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'text',
            'name'  => 'adEndTime',
            'attributes'    => [
                'id'    => 'adEndTime',
                'class' => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'checkbox',
            'name'  => 'adState',
            'attributes'    => [
                'id'    => 'adState',
                'value' => 1
            ]
        ]);
    }

    protected function addInputFilter()
    {
        $inputFilter = $this->getInputFilter();

        $inputFilter->add([
            'name'      => 'adUrl',
            'required'  => false,
            'filters'   => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ]
        ]);

        $inputFilter->add([
            'name'      => 'adWords',
            'required'  => false,
            'filters'   => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ]
        ]);

        $inputFilter->add([
            'type'      => 'Laminas\InputFilter\FileInput',
            'name'      => 'adImage',
            'required'  => false,
            'validators'=> [
                ['name' => 'FileUploadFile'],
                ['name' => ImageSuffixValidator::class],
                [
                    'name' => 'FileMimeType',
                    'options' => [
                        'mimeType' => Common::configValue('image_type')
                    ]
                ],
                ['name' => 'FileIsImage'],
                [
                    'name' => 'FileSize',
                    'options' => Common::configValue('upload_image_max')
                ]
            ],
            'filters'  => [
                [
                    'name' => 'FileRenameUpload',
                    'options' => [
                        'target' => Config::AD_IMAGE_PATH,
                        'useUploadName'=>true,
                        'useUploadExtension'=>true,
                        'overwrite'=>true,
                        'randomize'=>true
                    ]
                ],
                ['name' => ImageRegenerate::class],
                [
                    'name' => OldImage::class,
                    'options' => [
                        'oldImage' => $this->adInfo ? $this->adInfo->getAdBody() : ''
                    ]
                ]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'adStartTime',
            'required'  => false,
            'filters'   => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ]
        ]);

        $inputFilter->add([
            'name'      => 'adEndTime',
            'required'  => false,
            'filters'   => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ]
        ]);

        $inputFilter->add([
            'name'      => 'adState',
            'required'  => false,
            'validators'=> [
                [
                    'name'      => 'InArray',
                    'options'   => [
                        'haystack'  => [0, 1]
                    ]
                ]
            ]
        ]);
    }
}